{% extends "layout.html" %}
{% macro text_table_row(parameter, edit_text, show_text, validate, sort) -%}
<th data-field="{{ parameter }}" id="{{ parameter }}"
  {% if sort %}data-sortable="true" {% endif %}
  data-visible = "{{visiblility.get(parameter)}}"
  data-escape="true"
  {% if current_user.role_edit() %}
    data-editable-type="text"
    data-editable-url="{{ url_for('edit-book.edit_list_book', param=parameter)}}"
    data-editable-title="{{ edit_text }}"
    data-edit="true"
    {% if validate %}data-edit-validate="{{ _('This Field is Required') }}" {% endif %}
  {% endif %}
>{{ show_text }}</th>
{%- endmacro %}

{% macro book_checkbox_row(parameter, show_text, sort) -%}
<th data-name="{{parameter}}" data-field="{{parameter}}" data-switchable="false"
  {% if sort %}data-sortable="true" {% endif %}
  data-visible="{{visiblility.get(parameter)}}"
  data-formatter="bookCheckboxFormatter">
  {{show_text}}
</th>
{%- endmacro %}


{% block header %}
<link href="{{ url_for('static', filename='css/libs/bootstrap-table.min.css') }}" rel="stylesheet">
<link href="{{ url_for('static', filename='css/libs/bootstrap-editable.css') }}" rel="stylesheet">
<link href="{{ url_for('static', filename='css/libs/bootstrap-wysihtml5-0.0.3.css') }}" rel="stylesheet">
<style>
  .pagination {
      position: static !important;
      margin-top: 0.8rem !important;
  }
</style>
{% endblock %}
{% block body %}
<div class="discover">
  <h2 class="{{page}}">{{_(title)}}</h2>
    <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
    <div class="row form-group" style="display: inline-block;">
      <div class="btn btn-default disabled" id="merge_books" aria-disabled="true">
        {{_('Merge')}}
      </div>
      <div class="btn btn-default disabled" id="edit_selected_books" aria-disabled="true">
        {{_('Edit')}}
      </div>
      <div class="btn btn-default disabled" id="archive_selected_books" aria-disabled="true">
        {{_('Archive')}}
      </div>
      <div class="btn btn-default disabled" id="unarchive_selected_books" aria-disabled="true">
        {{_('Unarchive')}}
      </div>
      <div class="btn btn-default disabled" id="read_selected_books" aria-disabled="true">
        {{_('Mark as read')}}
      </div>
      <div class="btn btn-default disabled" id="unread_selected_books" aria-disabled="true">
        {{_('Mark as unread')}}
      </div>
      <div class="btn btn-default disabled" id="delete_selected_books" aria-disabled="true">
        {{_('Delete')}}
      </div>
      <div class="btn btn-default disabled" id="add_to_shelf_btn" aria-disabled="true">
        {{_('Add to Shelf')}}
      </div>
        <div class="btn btn-default disabled" id="table_xchange" ><span class="glyphicon glyphicon-arrow-up"></span><span class="glyphicon glyphicon-arrow-down"></span>{{_(' Exchange author & title')}}
      </div>
      <!-- New Select all button -->
      {% if current_user.role_edit() %}
      <div class="btn btn-default" id="select_all">
        {{ _('Select all') }}
      </div>
      <div class="btn btn-default" id="unselect_all">
        {{ _('Clear all') }}
      </div>
      {% endif %}
    </div>
    <div>
      <br>
      <div class="filterhead" style="display: inline-block;">
        <div style="display: inline-block; padding-inline-start: 10px;">
          <input type="checkbox" id="autoupdate_titlesort" name="autoupdate_titlesort" checked>
          <label for="autoupdate_titlesort" style="padding-left: 0.4em">{{_('Update Title Sort automatically  ')}}</label>
        </div>
        <div style="display: inline-block; padding-inline-start: 10px;">
          <input type="checkbox" id="autoupdate_authorsort" name="autoupdate_authorsort" checked>
          <label for="autoupdate_authorsort" style="padding-left: 0.4em">{{_('Update Author Sort automatically')}}</label>
        </div>
      </div>
    </div>

    <div>
      <table id="books-table" class="table table-no-bordered table-striped"
        data-url="{{url_for('web.list_books')}}" data-locale="{{ current_user.locale }}" data-classes="table table-no-bordered table-hover">
        <thead>
          <tr>
            {% if current_user.role_edit() %}
              <th data-field="state" data-checkbox="true" data-sortable="true"></th>
            {% endif %}
              <th data-field="id" id="id" data-visible="false" data-switchable="false"></th>
              {{ text_table_row('title', _('Enter Title'),_('Title'), true, true) }}
              {{ text_table_row('sort', _('Enter Title Sort'),_('Title Sort'), false, true) }}
              {{ text_table_row('author_sort', _('Enter Author Sort'),_('Author Sort'), false, true) }}
              {{ text_table_row('authors', _('Enter Authors'),_('Authors'), true, true) }}
              {{ text_table_row('tags', _('Enter Categories'),_('Categories'), false, true) }}
              {{ text_table_row('series', _('Enter Series'),_('Series'), false, true) }}
              <th data-field="series_index" id="series_index" data-visible="{{visiblility.get('series_index')}}" data-edit-validate="{{ _('This Field is Required') }}" data-sortable="true" {% if current_user.role_edit() %} data-editable-type="number" data-editable-placeholder="1" data-editable-step="0.01" data-editable-min="0" data-editable-url="{{ url_for('edit-book.edit_list_book', param='series_index')}}" data-edit="true" data-editable-title="{{_('Enter Title')}}"{% endif %}>{{_('Series ID')}}</th>
              {{ text_table_row('languages', _('Enter Languages'),_('Languages'), false, true) }}
              <!--th data-field="pubdate" data-type="date" data-visible="{{visiblility.get('pubdate')}}" data-viewformat="dd.mm.yyyy" id="pubdate" data-sortable="true">{{_('Publishing Date')}}</th-->
              {{ text_table_row('publishers', _('Enter Publishers'),_('Publishers'), false, true) }}
              <th data-field="comments" id="comments" data-escape="true" data-editable-mode="popup"  data-visible="{{visiblility.get('comments')}}" data-sortable="false" {% if current_user.role_edit() %} data-editable-type="wysihtml5" data-editable-url="{{ url_for('edit-book.edit_list_book', param='comments')}}" data-edit="true" data-editable-title="{{_('Enter comments')}}"{% endif %}>{{_('Comments')}}</th>
              {% if current_user.check_visibility(32768) %}
                {{ book_checkbox_row('is_archived', _('Archived?'), false)}}
              {%  endif %}
              {{ book_checkbox_row('read_status', _('Read?'), false)}}
              {% for c in cc %}
                {% if c.datatype == "int" %}
                  <th data-field="custom_column_{{ c.id|string }}" id="custom_column_{{ c.id|string }}" data-visible="{{visiblility.get('custom_column_'+ c.id|string)}}" data-sortable="false" {% if current_user.role_edit() %} data-editable-type="number" data-editable-placeholder="1" data-editable-step="1" data-editable-url="{{ url_for('edit-book.edit_list_book', param='custom_column_'+ c.id|string)}}" data-edit="true" data-editable-title="{{_('Enter ') + c.name}}"{% endif %}>{{c.name}}</th>
                {% elif c.datatype == "rating" %}
                  <th data-field="custom_column_{{ c.id|string }}" id="custom_column_{{ c.id|string }}" data-formatter="ratingFormatter" data-visible="{{visiblility.get('custom_column_'+ c.id|string)}}" data-sortable="false" {% if current_user.role_edit() %} data-editable-type="number" data-editable-placeholder="1" data-editable-step="0.5" data-editable-step="1" data-editable-min="1" data-editable-max="5" data-editable-url="{{ url_for('edit-book.edit_list_book', param='custom_column_'+ c.id|string)}}" data-edit="true" data-editable-title="{{_('Enter ') + c.name}}"{% endif %}>{{c.name}}</th>
                {% elif c.datatype == "float" %}
                  <th data-field="custom_column_{{ c.id|string }}" id="custom_column_{{ c.id|string }}" data-visible="{{visiblility.get('custom_column_'+ c.id|string)}}" data-sortable="false" {% if current_user.role_edit() %} data-editable-type="number" data-editable-placeholder="1" data-editable-step="0.01" data-editable-url="{{ url_for('edit-book.edit_list_book', param='custom_column_'+ c.id|string)}}" data-edit="true" data-editable-title="{{_('Enter ') + c.name}}"{% endif %}>{{c.name}}</th>
                {% elif c.datatype == "enumeration" %}
                  <th data-field="custom_column_{{ c.id|string }}" id="custom_column_{{ c.id|string }}" data-visible="{{visiblility.get('custom_column_'+ c.id|string)}}" data-sortable="false" {% if current_user.role_edit() %} data-editable-type="select" data-editable-source={{ url_for('edit-book.table_get_custom_enum', c_id=c.id)  }} data-editable-url="{{ url_for('edit-book.edit_list_book', param='custom_column_'+ c.id|string)}}" data-edit="true" data-editable-title="{{_('Enter ') + c.name}}"{% endif %}>{{c.name}}</th>
                {% elif c.datatype in ["datetime"] %}
                    <!-- missing -->
                {% elif c.datatype == "text" %}
                  {{ text_table_row('custom_column_' + c.id|string, _('Enter ') + c.name, c.name, false, false) }}
                {% elif c.datatype == "comments" %}
                    <th data-field="custom_column_{{ c.id|string }}" id="custom_column_{{ c.id|string }}" data-escape="true" data-editable-mode="popup"  data-visible="{{visiblility.get('custom_column_'+ c.id|string)}}" data-sortable="false" {% if current_user.role_edit() %} data-editable-type="wysihtml5" data-editable-url="{{ url_for('edit-book.edit_list_book', param='custom_column_'+ c.id|string)}}" data-edit="true" data-editable-title="{{_('Enter ') + c.name}}"{% endif %}>{{c.name}}</th>
                {% elif c.datatype == "bool" %}
                    {{ book_checkbox_row('custom_column_' + c.id|string, c.name, false)}}
                {% else %}
                  <!--{{ text_table_row('custom_column_' + c.id|string, _('Enter ') + c.name, c.name, false, false) }} -->
                {% endif %}
              {% endfor %}
            {% if current_user.role_delete_books() and current_user.role_edit()%}
              <th data-align="right" data-formatter="EbookActions" data-switchable="false">
                <br>
                {{_('Delete')}}
              </th>
            {% endif %}
          </tr>
        </thead>
      </table>
    </div>
</div>

{% endblock %}
{% block modal %}
{{ delete_book(current_user.role_delete_books()) }}
{% if current_user.role_edit() %}
<div class="modal fade" id="mergeModal" role="dialog" aria-labelledby="metaMergeLabel">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header bg-danger text-center">
        <span>{{_('Are you really sure?')}}</span>
      </div>
        <div class="modal-body">
          <p></p>
            <div class="text-left">{{_('Books with Title will be merged from:')}}</div>
          <p></p>
            <div class="text-left" id="merge_from"></div>
          <p></p>
            <div class="text-left">{{_('Into Book with Title:')}}</div>
          <p></p>
            <div class="text-left" id="merge_to"></div>
        </div>
      <div class="modal-footer">
        <input id="merge_confirm" type="button" class="btn btn-danger" value="{{_('Merge')}}" name="merge_confirm" data-dismiss="modal">
        <button id="merge_abort" type="button" class="btn btn-default" data-dismiss="modal">{{_('Cancel')}}</button>
      </div>
    </div>
  </div>
</div>

<div class="modal fade" id="delete_selected_modal" role="dialog" aria-labelledby="metaDeleteSelectedLabel">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header bg-danger text-center">
        <span>{{_('Are you really sure?')}}</span>
      </div>
      <div class="modal-body">
        <p></p>
          <div class="text-left">{{_('The following books will be deleted:')}}</div>
        <p></p>
          <div class="text-left" id="display-delete-selected-books"></div>
      <div class="modal-footer">
        <input id="delete_selected_confirm" type="button" class="btn btn-danger" value="{{_('Delete')}}" name="delete_selected_confirm" data-dismiss="modal">
        <button id="delete_selected_abort" type="button" class="btn btn-default" data-dismiss="modal">{{_('Cancel')}}</button>
      </div>
      </div>
    </div>
  </div>
</div>

<div class="modal fade" id="archive_selected_modal" role="dialog" aria-labelledby="metaArchiveSelectedLabel">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header bg-danger text-center">
        <span>{{_('Are you really sure?')}}</span>
      </div>
      <div class="modal-body">
        <p></p>
          <div class="text-left">{{_('The following books will be archived:')}}</div>
        <p></p>
          <div class="text-left" id="display-archive-selected-books"></div>
      <div class="modal-footer">
        <input id="archive_selected_confirm" type="button" class="btn btn-danger" value="{{_('Archive')}}" name="archive_selected_confirm" data-dismiss="modal">
        <button id="archive_selected_abort" type="button" class="btn btn-default" data-dismiss="modal">{{_('Cancel')}}</button>
      </div>
      </div>
    </div>
  </div>
</div>

<div class="modal fade" id="unarchive_selected_modal" role="dialog" aria-labelledby="metaUnArchiveSelectedLabel">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header bg-danger text-center">
        <span>{{_('Are you really sure?')}}</span>
      </div>
      <div class="modal-body">
        <p></p>
          <div class="text-left">{{_('The following books will be unarchived:')}}</div>
        <p></p>
          <div class="text-left" id="display-unarchive-selected-books"></div>
      <div class="modal-footer">
        <input id="unarchive_selected_confirm" type="button" class="btn btn-danger" value="{{_('Unarchive')}}" name="unarchive_selected_confirm" data-dismiss="modal">
        <button id="unarchive_selected_abort" type="button" class="btn btn-default" data-dismiss="modal">{{_('Cancel')}}</button>
      </div>
      </div>
    </div>
  </div>
</div>

<div class="modal fade" id="read_selected_modal" role="dialog" aria-labelledby="metaReadSelectedLabel">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header bg-danger text-center">
        <span>{{_('Are you really sure?')}}</span>
      </div>
      <div class="modal-body">
        <p></p>
          <div class="text-left">{{_('The following books will be marked read:')}}</div>
        <p></p>
          <div class="text-left" id="display-read-selected-books"></div>
      <div class="modal-footer">
        <input id="read_selected_confirm" type="button" class="btn btn-danger" value="{{_('Mark as read')}}" name="read_selected_confirm" data-dismiss="modal">
        <button id="read_selected_abort" type="button" class="btn btn-default" data-dismiss="modal">{{_('Cancel')}}</button>
      </div>
      </div>
    </div>
  </div>
</div>

<div class="modal fade" id="unread_selected_modal" role="dialog" aria-labelledby="metaReadSelectedLabel">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header bg-danger text-center">
        <span>{{_('Are you really sure?')}}</span>
      </div>
      <div class="modal-body">
        <p></p>
          <div class="text-left">{{_('The following books will be marked unread:')}}</div>
        <p></p>
          <div class="text-left" id="display-unread-selected-books"></div>
      <div class="modal-footer">
        <input id="unread_selected_confirm" type="button" class="btn btn-danger" value="{{_('Mark as unread')}}" name="unread_selected_confirm" data-dismiss="modal">
        <button id="read_selected_abort" type="button" class="btn btn-default" data-dismiss="modal">{{_('Cancel')}}</button>
      </div>
      </div>
    </div>
  </div>
</div>

<div class="modal fade" id="edit_selected_modal" role="dialog" aria-labelledby="metaEditSelectedLabel">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header text-center">
        <span>{{_('Edit Metadata')}}</span>
      </div>
      <div class="modal-body">
        <div class="text-left">{{_('Edit the fields you want changed. Blank fields will be ignored:')}}</div>
          <br>
          Title:
          <input class="form-control" id="title_input">
          <p></p>

          Title Sort:
          <input class="form-control" id="title_sort_input">
          <p></p>

          Author Sort:
          <input class="form-control" id="author_sort_input">
          <p></p>

          Authors:
          <input class="form-control" id="authors_input">
          <p></p>

          Categories:
          <input class="form-control" id="categories_input">
          <p></p>

          Series:
          <input class="form-control" id="series_input">
          <p></p>

          Languages:
          <input class="form-control" id="languages_input">
          <p></p>

          Publishers:
          <input class="form-control" id="publishers_input">
          <p></p>

          Comments:
          <input class="form-control" id="comments_input">
          <p></p>
        </div>

      <div class="modal-footer">
        <input id="edit_selected_confirm" type="button" class="btn btn-danger" value="{{_('Edit')}}" name="edit_selected_confirm" id="edit_selected_confirm" data-dismiss="modal">
        <button id="edit_selected_abort" type="button" class="btn btn-default" data-dismiss="modal">{{_('Cancel')}}</button>
      </div>
      </div>
    </div>
  </div>
</div>
{% endif %}

<!-- Add to Shelf Modal -->
<div class="modal fade" id="addToShelfModal" role="dialog" aria-labelledby="addToShelfModalLabel">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="{{_('Close')}}"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="addToShelfModalLabel">{{_('Select a Shelf')}}</h4>
      </div>
      <div class="modal-body">
        <p>{{_('Please select a shelf to add the selected books to:')}}</p>
        <select class="form-control" id="shelf_selection_dropdown">
            <option value="">{{_('-- Select a Shelf --')}}</option>
            {% if g.shelves_access %}
                {% for shelf in g.shelves_access %}
                    {% if current_user.is_anonymous == False %}
                        {% if shelf.user_id == current_user.id or (shelf.is_public and current_user.role_edit_shelfs()) %}
                            <option value="{{ shelf.id }}">{{ shelf.name }}</option>
                        {% endif %}
                    {% elif shelf.is_public and config.config_anon_browse %}
                        {# Anonymous users can only see public shelves if anon browse is enabled #}
                        {# Permissions to edit public shelves for anonymous would be odd, so we assume they can't add to public shelves #}
                        {# This part might need adjustment based on how `check_shelf_edit_permissions` handles anonymous users for public shelves #}
                    {% endif %}
                {% endfor %}
            {% endif %}
        </select>
      </div>
      <div class="modal-footer">
        <button id="cancel_add_to_shelf_btn" type="button" class="btn btn-default" data-dismiss="modal">{{_('Cancel')}}</button>
        <input id="confirm_add_to_shelf_btn" type="button" class="btn btn-primary" value="{{_('Add')}}">
      </div>
    </div>
  </div>
</div>
{% endblock %}

{% block js %}
<script src="{{ url_for('static', filename='js/libs/bootstrap-table/bootstrap-table.min.js') }}"></script>
<script src="{{ url_for('static', filename='js/libs/bootstrap-table/bootstrap-table-locale-all.min.js') }}"></script>
<script src="{{ url_for('static', filename='js/libs/bootstrap-table/bootstrap-table-editable.min.js') }}"></script>
<script src="{{ url_for('static', filename='js/libs/bootstrap-table/bootstrap-editable.min.js') }}"></script>
{% if not current_user.locale == 'en' %}
<script src="{{ url_for('static', filename='js/libs/bootstrap-table/locale/bootstrap-table-' + current_user.locale + '.min.js') }}" charset="UTF-8"></script>
{% endif %}
<script src="{{ url_for('static', filename='js/libs/wysihtml5-0.3.0.min.js') }}"></script>
<script src="{{ url_for('static', filename='js/libs/bootstrap-wysihtml5-0.0.3.min.js') }}"></script>
<script src="{{ url_for('static', filename='js/libs/wysihtml5-0.0.3.js') }}"></script>
<script src="{{ url_for('static', filename='js/table.js') }}"></script>
{% endblock %}